/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import * as fse from 'fs-extra';
import path from 'path';

import { DecipherUtil } from './decipher-util';
import { FileUtil } from './file-util';
import { KeytoolUtils } from './keytool-utils';

/**
 * 密钥库相关操作的工具类
 *
 * @since 2022/5/9
 */
export class KeyStoreHelper {
  private static CERTIFICATE_DESC = 'CN=debugkey';
  private static DEFAULT_IDE_CONFIG_PATH = '.ohos';
  private static CONFIG = 'config';
  private static DEFAULT_DEBUG_SIGN_FILE = 'debug.p12';
  private static keyBitsLength = 3072;
  private static DEFAULT_STORE_PASS_FILE = '../../res/material/zb/de';
  public static DEFAULT_STORE_PASS = KeyStoreHelper.loadDefaultStorePass();
  public static DEFAULT_ALIAS = 'debugKey';
  private static validity = 365;
  private static validFrom = new Date();
  private static keyAlgo = 'RSA';
  private static signatureAlgo = 'SHA256withRSA';
  private static requestCertFile = 'requestCert.req';
  private static generateCertFile = 'generateCert.gen';

  /**
   * 获得默认的密钥库地址
   */
  static getDefaultDebugKeyStoreLocation(): string | undefined {
    const userHome = process.env.HOME || process.env.USERPROFILE;
    if (userHome === undefined) {
      return undefined;
    }
    const configDir = path.resolve(userHome, KeyStoreHelper.DEFAULT_IDE_CONFIG_PATH, KeyStoreHelper.CONFIG);
    if (!fse.existsSync(configDir)) {
      fse.mkdirsSync(configDir);
    }
    return path.resolve(configDir, KeyStoreHelper.DEFAULT_DEBUG_SIGN_FILE);
  }

  /**
   * 创建默认的密钥库
   */
  static createDefaultDebugStore() {
    const storeFile = KeyStoreHelper.getDefaultDebugKeyStoreLocation();
    if (storeFile === undefined || fse.existsSync(storeFile)) {
      return;
    }
    if (!KeyStoreHelper.createDebugStore(storeFile, KeyStoreHelper.DEFAULT_STORE_PASS, KeyStoreHelper.DEFAULT_ALIAS)) {
      FileUtil.deleteFile(KeyStoreHelper.requestCertFile);
      FileUtil.deleteFile(KeyStoreHelper.generateCertFile);
    }
  }

  private static createDebugStore(storeFile: string, storePassword: string, keyAlias: string) {
    return KeyStoreHelper.createNewStore(storeFile, storePassword, keyAlias, KeyStoreHelper.CERTIFICATE_DESC);
  }

  private static createNewStore(storeFile: string, storePassword: string, keyAlias: string, dn: string) {
    let res: boolean;
    const keytoolUtils = new KeytoolUtils(storeFile, storePassword, keyAlias, dn);
    res = keytoolUtils.generateKeyPair(
      KeyStoreHelper.validity,
      KeyStoreHelper.keyBitsLength,
      KeyStoreHelper.keyAlgo,
      KeyStoreHelper.signatureAlgo,
      KeyStoreHelper.validFrom,
    );
    if (!res) return res;
    res = keytoolUtils.requestCert(KeyStoreHelper.requestCertFile);
    if (!res) return res;
    res = keytoolUtils.generateCert(
      true,
      KeyStoreHelper.validity,
      KeyStoreHelper.signatureAlgo,
      KeyStoreHelper.validFrom,
      KeyStoreHelper.requestCertFile,
      KeyStoreHelper.generateCertFile,
    );
    if (!res) return res;
    res = keytoolUtils.importCert(true, KeyStoreHelper.generateCertFile);
    return res;
  }

  private static loadDefaultStorePass() {
    return DecipherUtil.decryptPluginDataFromResource(KeyStoreHelper.DEFAULT_STORE_PASS_FILE);
  }
}
